<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<head>
    <title>Title</title>
    <%@include file="../../common.jsp"%>
</head>
<style>
    body{
        font-size: 12px;
    }
    #taskTable tr{
        font-size: 12px;
    }
</style>
<body>
<h2>巡检任务</h2>
<hr>
    <form class="layui-form" action="">
        <div class="layui-form-item">

            <div class="layui-inline">
                <label class="layui-form-label">任务编号:</label>
                <div class="layui-input-inline" style="width: 200px;">
                    <input id="taskId" type="text" name="price_min" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">线路编号:</label>
                <div class="layui-input-inline" style="width: 200px;">
                    <input id="lineCode" type="text" name="price_min" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">任务状态:</label>
                <div class="layui-input-inline">
                    <select id="taskStateId">
                        <option value="-1">请选择</option>
                        <option value="1">待分配</option>
                        <option value="2">已分配</option>
                        <option value="3">执行中</option>
                        <option value="4">已完成</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">下发人:</label>
                <div class="layui-input-inline">
                    <input id="issuedByName" type="text" name="price_min" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">下发时间:</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" id="startTime" placeholder="yyyy-MM-dd">
                </div>
                <div class="layui-form-label" style="width:5px">-</div>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input testl" id="endTime" placeholder="yyyy-MM-dd">
                </div>


                <div class="layui-input-inline" style="width: 100px;">
                    <button type="button" class="layui-btn layui-btn-normal" onclick="searchData();"><i class="layui-icon layui-icon-search"></i> 查询</button>
                </div>
            </div>

        </div>
    </form>
    <table class="layui-table" id="taskTable" lay-filter="taskTable"></table>

    <script>

        var taskId;//巡检任务编号,用户查看任务详情
        //全局定义一次, 加载formSelects
        layui.config({
            base: '${path}/layui/lay/modules/' //此处路径请自行处理, 可以使用绝对路径
        }).extend({
            formSelects: 'formSelects-v4.min'
        });


       function searchData(){
            layui.table.reload("taskTable",{
                page:{
                    curr : 1
                },
                where:{
                    "inspectionTaskCode":$("#taskId").val(),
                    "lineCode" : $("#lineCode").val(),
                    "issuedByName" : $("#issuedByName").val(),
                    "startTime" : $("#startTime").val(),
                    "endTime" : $("#endTime").val(),
                    "taskStateId" : $("#taskStateId").val()
                }
            });
        }


        layui.use(["table","form","formSelects","laydate"],function () {
            var table = layui.table;
            table.render({
                id:'taskTable',
                elem:'#taskTable',
                url:'/task/list',
                page:true,
                even:false,
                toolbar:'#toolbarDemo',
                cols:[[
                    {field:'inspectionTaskCode', title:'任务编号'},
                    {field:'inspectionTaskName', title:'任务名称'},
                    {field:'startPoleCode', title:'起始杆号'},
                    {field:'endPoleCode', title:'终止杆号'},
                    {field:'issuedByName', title:'下发人'},
                    {field:'issuedTime', title:'下发时间'},
                    {field:'taskStateName', title:'任务状态'}, ,
                    {field:'isStart', title:'任务是否取消',templet:'#lockedTem'},
                    {field:'taskStateId',title:'操作',width:'230',templet:function (d) {
                            var select = "blue";
                            var fenpei = "blue";
                            var update = "blue";
                            var del = "blue";
                            //行内工具栏
                            var fenpeiEvent = 'lay-event="fenpei"';
                            var updateEvent = 'lay-event="edit"';
                            var delEvent = 'lay-event="del"';
                            if(d.taskStateId == 4 || d.taskStateId == 3 || d.taskStateId == 2 || d.isCancel == 1) {
                                fenpei = "#999999";
                                update = "#999999";
                                del = "#999999";
                                fenpeiEvent = "";
                                updateEvent = "";
                                delEvent = "";
                            }
                            var a = '<a href="javascript:void(0)" lay-event="selectTask"  style=color:'+select+'>查看</a> | ' +
                                '<a href="javascript:void(0)" '+fenpeiEvent+'  style=color:'+fenpei+'>分配任务</a> | ' +
                                '<a href="javascript:void(0)" '+updateEvent+' style=color:'+update+'>修改</a> | ' +
                                '<a href="javascript:void(0)" '+delEvent+' style=color:'+del+'>取消</a>';
                            return a;
                        }}
                ]]
            })



            table.on('toolbar(taskTable)',function (obj) {
                var e = obj.event;
                switch (e) {
                    case "add":
                        $.get('${path}/task/form',function (str) {
                            layer.open({
                                type:1,
                                title:'制定巡检任务',
                                area:'700px',
                                content: str,
                                btn: ['确定','取消'],
                                success:function(){
                                    //赋值巡检员下拉框
                                    $.get("${path}/inspector/getNormalInspector",function (data) {
                                        $.each(data,function () {
                                            var option = $('<option></option>').appendTo("#inspectorStr");
                                            option.text(this.inspectorName).val(this.id);
                                            layui.formSelects.render();
                                        });
                                    });
                                    //赋值正常线路下拉框
                                    $.get("${path}/line/getNormalLine",function (data) {
                                        $.each(data,function () {
                                            var option = $('<option></option>').appendTo("#line");
                                            option.text(this.lineName).val(this.id);
                                        });
                                        layui.form.render();
                                    });
                                    //下拉框选择事件,自动返回起始杆号与终止杆号
                                    layui.form.on('select(line)', function(data){
                                        var lineId = data.value;
                                        //根据线路编号查询线路信息
                                        $.get("line/getLine",{"lineId":lineId},function (data) {
                                            //赋值起始杆号和终止杆号
                                            $(":input[name = 'lineId']").val(lineId);
                                            $(":input[name = 'lineName']").val(data.lineName);
                                            $(":input[name = 'lineCode']").val(data.lineCode);
                                            $(":input[name = 'startPoleCode']").val(data.startPoleCode);
                                            $(":input[name = 'endPoleCode']").val(data.endPoleCode);
                                            $(":input[name = 'startPoleId']").val(data.startPoleId);
                                            $(":input[name = 'endPoleId']").val(data.endPoleId);
                                        })
                                    });
                                },
                                yes : function (index) {
                                    var inspector = layui.formSelects.value('inspector','valStr');
                                    //添加巡检任务
                                    //获取巡检员字符串
                                    var inspectors = layui.formSelects.value('inspector','nameStr');
                                    $(":input[name = 'inspectors']").val(inspectors);

                                    var params = $("#taskForm").serialize();
                                    $.post('${path}/task/add',params,function () {
                                        layer.close(index);
                                        layer.msg('添加成功!',{icon:1})
                                        table.reload('taskTable');
                                    });
                                }
                            });
                        });
                        break;
                    case "delete":
                        var rows = table.checkStatus('psInspectiontask_mainTable');
                        var data = rows.data;
                        if(data.length == 0){
                            layer.msg("请选择要删除的数据",function (){});
                            return;
                        }
                        layer.confirm('确定删除吗？',{icon:5,title:'友情提示'},function (index) {
                           var params = "";
                           for (let pole of data){
                               params+= "ids="+pole.id+"&";
                           }
                            $.post('${path}/psPole/delete',params,function () {
                                layer.close(index);
                                table.reload('poleTable');
                            })
                        });
                        break;

                }
            });

            //监听 行
            table.on("tool(taskTable)",function (obj) {
                var data = obj.data;
                if (obj.event == 'edit'){
                    $.get("${path}/task/form",function (str) {
                        layer.open({
                            type:1,
                            title:'修改巡检任务',
                            area:'700px',
                            content: str,
                            btn: ['确定','取消'],
                            success:function(){
                                //根据任务编号获取任务信息
                                $.get("/task/getTaskId",{"taskId":obj.data.id},function (data) {
                                    layui.form.val("taskForm",data);
                                    var option = $('<option></option>').appendTo("#line");
                                    option.text(data.lineName).val(data.lineId);
                                    //选中当前线路
                                    $(":input[name = 'lineId']").val(data.lineId);
                                    layui.form.render();
                                })

                                //赋值巡检员下拉框
                                $.get("${path}/inspector/getNormalInspector",function (data) {
                                    $.each(data,function () {
                                        var option = $('<option></option>').appendTo("#inspectorStr");
                                        option.text(this.inspectorName).val(this.id);
                                        layui.formSelects.render();
                                    });
                                });
                                //赋值正常线路下拉框
                                $.get("${path}/line/getNormalLine",function (data) {
                                    $.each(data,function () {
                                        var option = $('<option></option>').appendTo("#line");
                                        option.text(this.lineName).val(this.id);
                                    });
                                    layui.form.render();
                                });


                                //下拉框选择事件,自动返回起始杆号与终止杆号
                                layui.form.on('select(line)', function(data){
                                    var lineId = data.value;
                                    //根据线路编号查询线路信息
                                    $.get("line/getLine",{"lineId":lineId},function (data) {
                                        //赋值起始杆号和终止杆号
                                        $(":input[name = 'lineId']").val(lineId);
                                        $(":input[name = 'lineName']").val(data.lineName);
                                        $(":input[name = 'lineCode']").val(data.lineCode);
                                        $(":input[name = 'startPoleCode']").val(data.startPoleCode);
                                        $(":input[name = 'endPoleCode']").val(data.endPoleCode);
                                        $(":input[name = 'startPoleId']").val(data.startPoleId);
                                        $(":input[name = 'endPoleId']").val(data.endPoleId);
                                    })
                                });
                            },
                            yes : function (index) {
                                var inspector = layui.formSelects.value('inspector','valStr');
                                //获取巡检员字符串
                                var inspectors = layui.formSelects.value('inspector','nameStr');
                                $(":input[name = 'inspectors']").val(inspectors);
                               //修改巡检任务
                                var params = $("#taskForm").serialize();
                                $.post('${path}/task/update',params,function () {
                                    layer.close(index);
                                    layer.msg('修改成功!',{icon:1})
                                    table.reload('taskTable');
                                });

                            }
                        });
                    });
                }else if(obj.event == 'fenpei'){
                    $.get("${path}/task/form",function (str) {
                        layer.open({
                            type:1,
                            title:'分配巡检任务',
                            area:'700px',
                            content: str,
                            btn: ['确定','取消'],
                            success:function(){
                                //根据任务编号获取任务信息
                                $.get("/task/getTaskId",{"taskId":obj.data.id},function (data) {
                                    layui.form.val("taskForm",data);
                                    var option = $('<option></option>').appendTo("#line");
                                    option.text(data.lineName).val(data.lineId);
                                    //选中当前线路
                                    $(":input[name = 'lineId']").val(data.lineId);
                                })

                                //赋值巡检员下拉框
                                $.get("${path}/inspector/getNormalInspector",function (data) {
                                    $.each(data,function () {
                                        var option = $('<option></option>').appendTo("#inspectorStr");
                                        option.text(this.inspectorName).val(this.id);
                                        layui.formSelects.render();
                                    });
                                });
                                //赋值正常线路下拉框
                                $.get("${path}/line/getNormalLine",function (data) {
                                    $.each(data,function () {
                                        var option = $('<option></option>').appendTo("#line");
                                        option.text(this.lineName).val(this.id);
                                    });
                                    layui.form.render();
                                });
                                //禁用所有文本框,巡检员下拉框除外
                                $(":input[name = 'inspectionTaskCode']").attr("readonly","readonly");
                                $(":input[name = 'inspectionTaskName']").attr("readonly","readonly");
                                $(":input[name = 'lineId']").attr("disabled","disabled");
                                $(":input[name = 'startPoleCode']").attr("readonly","readonly");
                                $(":input[name = 'endPoleCode']").attr("readonly","readonly");
                                $(":input[name = 'issuedByName']").attr("readonly","readonly");
                                $(":input[name = 'time']").attr("readonly","readonly");
                                $(":input[name = 'common']").attr("readonly","readonly");
                            },
                            yes : function (index) {
                                var inspector = layui.formSelects.value('inspector','valStr');
                                //获取巡检员字符串
                                var inspectors = layui.formSelects.value('inspector','nameStr');
                                $(":input[name = 'inspectors']").val(inspectors);
                                //修改巡检任务
                                var params = $("#taskForm").serialize();
                                $.post('${path}/task/updateInspector',params,function () {
                                    layer.close(index);
                                    layer.msg('分配任务成功!',{icon:1})
                                    table.reload('taskTable');
                                });

                            }
                        });
                    });
                }else if(obj.event == 'selectTask'){
                    window.open("/task/InspectorDetialIndex?taskId="+obj.data.id,"middle");
                }else if(obj.event == 'del'){
                    layer.open({
                        time:0,
                        title:'友情提示',
                        content:'确定取消任务吗？',
                        btn:['确定','取消'],
                        yes : function () {
                            //修改任务状态,改变线路状态,改变巡检员状态,删除任务详细信息
                            $.get("/task/cancelTask",{"taskId":obj.data.id,"lineId":obj.data.lineId},function (data) {
                                layer.msg('取消任务成功!',{icon:1})
                                table.reload('taskTable');
                            })
                        }
                    })
                }
            })
        });

        layui.use('laydate',function () {
           var laydate = layui.laydate;

           laydate.render({
               elem:'#startTime'
           });
           laydate.render({
                elem:'#endTime'
           });
            laydate.render({
                elem: '#issuedTime'
                ,value: new Date()
                ,isInitValue: true
            });
        });

    </script>
    <script type="text/html" id="lockedTem">
        <input type="checkbox" disabled lay-skin="switch" title="是否取消" lay-text="是|否" {{d.isCancel == 1 ? 'checked' : ''}}>
    </script>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-circle"></i> 制定巡检任务</button>
        </div>
    </script>

</body>
</html>
